## creates tables for appendix H:
## analyzing ideological consistency
## figure A9 and table A8

rm(list=ls())

library(tidyverse)
library(kableExtra)


# read in and clean data

opinion_bootstrapped <- readRDS('bootstrap_gaps_data/opinion_bootstrapped_noleaners.RDS')

for (i in names(opinion_bootstrapped)) {
  assign(i, opinion_bootstrapped[[i]])
}

dem_polarization_boot <- dem_top_boot - dem_bottom_boot
rep_polarization_boot <- rep_top_boot - rep_bottom_boot
all_polarization_boot <- all_top_boot - all_bottom_boot

dem_issue_polarization <- abs(apply(dem_polarization_boot, MARGIN = 1, median))
rep_issue_polarization <- abs(apply(rep_polarization_boot, MARGIN = 1, median))
all_issue_polarization <- abs(apply(all_polarization_boot, MARGIN = 1, median))

dem_top_op <- apply(dem_top_boot, MARGIN = 1, median)
dem_bot_op <- apply(dem_bottom_boot, MARGIN = 1, median)
rep_top_op <- apply(rep_top_boot, MARGIN = 1, median)
rep_bot_op <- apply(rep_bottom_boot, MARGIN = 1, median)

diff_polarization_boot <- abs(rep_polarization_boot) - abs(dem_polarization_boot)
diff_polarization_by_issue <- apply(diff_polarization_boot, MARGIN = 1, median)

issueideology <- read.csv('issue_ideology.csv')



ideology_df <- data.frame(question = names(dem_top_op),
                         dem_top = dem_top_op,
                         dem_bot = dem_bot_op,
                         rep_top = rep_top_op,
                         rep_bot = rep_bot_op,
                         dem_pol = dem_issue_polarization,
                         rep_pol = rep_issue_polarization) %>% 
  left_join(issueideology) %>% 
  mutate(question = sub('_', ': ', question, fixed = TRUE))


# table A8 - top 10 largest gaps by ideology

ideology_tab <- matrix(NA, nrow = 2, ncol = 2, dimnames = list(c('dem', 'rep'), c('rich_conservative', 'rich_liberal')))

ideology_tab['dem', 'rich_conservative'] <- ideology_df %>% 
  filter((dem_top > dem_bot & liberal == 0) | (dem_bot > dem_top & liberal == 1),
         dem_pol > 0.1) %>% 
  arrange(-dem_pol) %>% 
  slice(1:10) %>% 
  pull(question) %>% paste(collapse = '\n') %>% linebreak(.)

ideology_tab['dem', 'rich_liberal'] <- ideology_df %>% 
  filter((dem_top > dem_bot & liberal == 1) | (dem_bot > dem_top & liberal == 0),
         dem_pol > 0.1) %>% 
  arrange(-dem_pol) %>% 
  slice(1:10) %>% 
  pull(question) %>% paste(collapse = '\n') %>% linebreak(.)

ideology_tab['rep', 'rich_conservative'] <- ideology_df %>% 
  filter((rep_top > rep_bot & liberal == 0) | (rep_bot > rep_top & liberal == 1),
         rep_pol > 0.1) %>% 
  arrange(-rep_pol) %>% 
  slice(1:10) %>% 
  pull(question) %>% paste(collapse = '\n') %>% linebreak(.)

ideology_tab['rep', 'rich_liberal'] <- ideology_df %>% 
  filter((rep_top > rep_bot & liberal == 1) | (rep_bot > rep_top & liberal == 0),
         rep_pol > 0.1) %>% 
  arrange(-rep_pol) %>% 
  slice(1:10) %>% 
  pull(question) %>% paste(collapse = '\n') %>% linebreak(.)

ideology_tab %>% 
  as.data.frame %>% 
  rownames_to_column('party') %>% 
  mutate(party = case_when(party == 'rep' ~ 'Republicans',
                           party == 'dem' ~ 'Democrats'),
         party = cell_spec(party, angle = 90, format = 'latex')) %>%
  kable('latex', booktabs = TRUE, escape = FALSE, linesep = '\\addlinespace',
        col.names = linebreak(c(' ', 'Top decile more conservative\nthan bottom decile', 
                                'Top decile more liberal\nthan bottom decile'),
                              align = 'c')) %>%
  column_spec(1:3, latex_valign = 'p') %>% 
  save_kable(file = 'appendix_tables/ideology_threshold_issues.tex')



# figure A9 - consistency by party and class

classideology.out <- list()

for (i in c('dem', 'rep', 'all')) {
  for (j in c('top', 'middle', 'bottom')) {
    tmp.df <- get(paste(i, j, 'boot', sep = '_'))
    classideology.out[[paste(i, j, sep = '_')]] <- as.data.frame(t(tmp.df)) %>% 
      gather(key = question, value = opinion) %>% 
      mutate(party = i, class = j)
  }
}

classideology_df <- bind_rows(classideology.out) %>% 
  left_join(issuetopics) %>% 
  left_join(issueideology) %>% 
  select(question, topic_6, opinion, party, class, liberal) %>% 
  mutate(lib_opinion = (liberal * opinion) + ((1-liberal) * (1-opinion)))

classideology_df <- classideology_df %>% 
  group_by(party, topic_6, class) %>% 
  summarize(lib_opinion = mean(lib_opinion)) %>% 
  ungroup() %>% 
  bind_rows(classideology_df %>% 
              group_by(party, class) %>% 
              summarize(lib_opinion = mean(lib_opinion)) %>% 
              ungroup() %>% 
              mutate(topic_6 = 'Total'))


ideologyplot2.df <- classideology_df %>% 
  filter(class %in% c('top', 'bottom')) %>% 
  mutate(cons_opinion = 1 - lib_opinion) %>% 
  gather(key = 'ideology', value = 'opinion', ends_with('_opinion')) %>% 
  filter(party == 'all' | (party == 'dem' & ideology == 'lib_opinion') | (party == 'rep' & ideology == 'cons_opinion'))

jitter <- 0.1

ideologyplot2.df$y <- as.numeric(factor(ideologyplot2.df$topic_6))
ideologyplot2.df$y <- ideologyplot2.df$y - jitter + (2 * (ideologyplot2.df$ideology == 'lib_opinion') * jitter)

ideologyplot2.df$topic_6 = factor(ideologyplot2.df$topic_6, levels = rev(unique(ideologyplot2.df$topic_6)))

ggplot(ideologyplot2.df %>% 
         filter(party != 'all'),
       aes(x = opinion, y = topic_6)) + 
  geom_point(aes(color = party, shape = class), size = 3) + 
  labs(x = 'Ideological Consistency', 
       y = '',
       color = 'Party',
       shape = 'Income Decile') +
  theme_bw() +
  scale_color_manual(breaks = c('all', 'dem', 'rep'),
                     values = c('gray40', 'blue', 'red'),
                     labels = c('All Respondents', 'Democrats', 'Republicans')) +
  scale_shape_manual(breaks = c('top', 'bottom'),
                     values = c(16, 1),
                     labels = c('Top', 'Bottom')) +
  scale_y_discrete(breaks = c('cultural', 'economic', 'foreignpolicy', 'immigration', 'lawenforcement', 'socialwelfare', 'Total'),
                   labels = c('Cultural', 'Economic', 'Foreign Policy', 'Immigration', 'Law Enforcement', 'Social Welfare', 'Total')) +
  theme(legend.position = 'bottom', 
        panel.grid.minor.y = element_blank(),
        # panel.grid.major.y = element_line(color = 'black'),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank())
ggsave('appendix_figures/ideology_points.png', width = 6, height = 4, scale = 1.1)
